import wx
import os.path
import __main__

from utils import tracer
from gui.toolbar import Toolbar
from projectBrowser import ProjectBrowser
from configPane import ConfigPane
from plugins.base import plugin

class ProjectManager(plugin.Plugin):
  def __init__(self):
    plugin.Plugin.__init__(self)

  def init(self, mainFrame, pluginManager):
    plugin.Plugin.init(self, mainFrame, pluginManager)

    if self._pluginMgr is None:
      return

    pluginInfo = self._pluginMgr.getPluginByName('DocumentManager')
    if pluginInfo is None:
      return

    mod = pluginInfo.getModule()
    if mod is None:
      return

    mod.documentChangedEventHandler(self.onDocumentChanged)
    mod.documentNameChangedEventHandler(self.onDocumentNameChanged)
  
  def uninit(self):
    pass
  
  def createPane(self):
    if self._mainFrm is None:
      return None

    pane = ProjectBrowser(self._mainFrm)
    pane.openFileEventHandler(self.onOpenFile)
    pane.gotoLineEventHandler(self.onGotoLine)
    return pane
  
  def createConfigPane(self):
    if self._mainFrm is None:
      return None
    return ConfigPane(self._mainFrm)
  
  def createMenus(self):
    if self._mainFrm is None:
      return None
    
    menus = []
  
    # create file menu
    proj_menu = wx.Menu()
    
    proj_menu_item_new = proj_menu.Append(-1, '&New')
    self._mainFrm.Bind(wx.EVT_MENU, self.onNewProject, proj_menu_item_new)

    proj_menu_item_open = proj_menu.Append(-1, '&Open')
    self._mainFrm.Bind(wx.EVT_MENU, self.onOpenProject, proj_menu_item_open)

    proj_menu_item_save = proj_menu.Append(-1, '&Save')
    self._mainFrm.Bind(wx.EVT_MENU, self.onSaveProject, proj_menu_item_save)

    proj_menu_item_separator_1 = proj_menu.AppendSeparator()

    proj_menu_item_delete = proj_menu.Append(-1, '&Delete Item')
    self._mainFrm.Bind(wx.EVT_MENU, self.onDeleteItem, proj_menu_item_delete)
    
    proj_menu_item_add_module = proj_menu.Append(-1, 'Add &Module')
    self._mainFrm.Bind(wx.EVT_MENU, self.onAddModule, proj_menu_item_add_module)

    proj_menu_item_add_package = proj_menu.Append(-1, 'Add &Package')
    self._mainFrm.Bind(wx.EVT_MENU, self.onAddPackage, proj_menu_item_add_package)

    menus.append(('&Project', proj_menu))
  
    return menus
  
  def createToolbar(self):
    pass

  def getMainScript(self):
    if self._pane is None:
      return None
    return self._pane.getMainScript()
  
  def onNewProject(self, evt):
    pass
  
  def onOpenProject(self, evt):
    self.openProject()
  
  def onSaveProject(self, evt):
    self.saveProject()
  
  def onAddModule(self, evt): 
    pane = self.getPane()
    if not pane is None:
      pane.addModule()

  def onDeleteItem(self, evt):
    pane = self.getPane()
    if not pane is None:
      pane.deleteSelectedItem()
  
  def onAddPackage(self, evt): 
    pane = self.getPane()
    if not pane is None:
      pane.addPackage()
  
  def openProject(self, project=''):  
    projBrowser = self.getPane()
    if projBrowser is None:
      return
    projBrowser.openProject(project)
  
  def saveProject(self):
    pane = self.getPane()
    if not pane is None:
      pane.saveProject()
  
  # -- pane event processers -------------------------------------------------  
  def onOpenFile(self, sender, args):
    assert not args is None
  
    filename = args.getValue()[0]
    tracer.writeln(filename)
    assert os.path.exists(filename)
    
    if self._pluginMgr is None:
      return
  
    pluginInfo = self._pluginMgr.getPluginByName('DocumentManager')
    if pluginInfo is None:
      return
  
    mod = pluginInfo.getModule()
    if mod is None:
      return
  
    mod.openFile(filename)
  
  def onGotoLine(self, sender, args):
    assert not args is None
  
    filename = args.getValue()[0]
    tracer.writeln(filename)
    assert os.path.exists(filename)
    
    lineNo = args.getValue()[1]
    tracer.writeln(lineNo)
    assert lineNo >= 0
  
    if self._pluginMgr is None:
      return
  
    pluginInfo = self._pluginMgr.getPluginByName('DocumentManager')
    if pluginInfo is None:
      return
  
    mod = pluginInfo.getModule()
    if mod is None:
      return
  
    mod.openFile(filename)
    mod.gotoLine(lineNo)

  def onDocumentChanged(self, sender, args):
    pane = self.getPane()
    if pane is None:
      return
    
    doc = args.getValue()[0]
    pane.refreshModuleInfo(doc.getFilename())
    

  def onDocumentNameChanged(self, sender, args):
    tracer.writeln('Project Manager received DocumentNameChanged event.')



instance = None
def getInstance():
  global instance
  if instance is None:
    instance = createInstance()
  return instance

def createInstance():
  return ProjectManager()
